import _ from "lodash"
import Store from "store2"
import moment from "moment"

let localStore = class {
  constructor() {

  }

  set(key, data) {
    Store(key, data);
  }

  get(key) {
    return Store.get(key)
  }

  remove(key) {
    Store.remove(key)
  }

  setUserInfor(data) {
    _.each(data, (item, key) => {
      !_.has({pwd: "", status: "", kouling: "", addtime: ""}, key) ? this.set(key, item) : null;
    });
    this.set("nextLoginTime", moment().add(2, 'd').format("MM-DD-YYYY"));
  }

  getUserInfor() {
    let Obj = {}, TimeOver = moment(this.get("nextLoginTime")).isAfter(moment().format("MM-DD-YYYY"), 'd');
    ['headimg', 'id', 'isAgency', 'mobile', 'name', 'pid'].forEach((key) => {
      Obj[key] = this.get(key);
    });
    return !TimeOver ? false : Obj
  }

  clearUserInfor() {
    ['headimg', 'id', 'isAgency', 'mobile', 'name', 'nextLoginTime', 'kouling', 'pid', 'addtime'].forEach((key) => {
      this.remove(key);
    });
  }

  saveSharelist(value) {
    if (Store.get("sharelist")) {
      let sharelist = Store.get("sharelist").toString().split(",");
      if (sharelist.indexOf(value.toString()) === -1) {
        if (sharelist.length >= 1) {
          value = `${sharelist},${value}`
        }
        Store("sharelist", value);
        return true
      } else {
        return false
      }
    } else {
      Store("sharelist", value);
      return true
    }
  }

  clearShareList(){
    this.remove("sharelist");
  }

  reduceShareList(value){
    return new Promise((resolve,reject) => {
       if(Store.get("sharelist")){
           let sharelist = Store.get("sharelist").toString().split(",");
           let index = sharelist.indexOf(value.toString());
           if(index !== -1){
              sharelist.splice(index,1);
           }
           this.set("sharelist",sharelist.toString());
           resolve()
       }else{
           reject()
       }
    })
  }
};

export default {
  install: function (Vue, options) {
    Vue.prototype.$localStore = new localStore()
  }
}

